<template>
<div class="dataset">
    <!-- 卡片视图 -->
    <el-card>
      <!-- 按钮 -->
      <el-row>
        <el-col :span="12">
          <el-form size="small" inline>
            <el-form-item label-width="40px">
              <el-input placeholder="请输入名称" v-model="param.name" clearable>
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="loadDataset"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="1" :offset="11">
          <el-form label-width="40px" label-position="right">
            <el-button type="primary" size="small" @click="toAddHandler"
              >新增</el-button
            >
          </el-form>
        </el-col>
      </el-row>
      <!-- /按钮 -->
      <!-- {{dataset}} -->
      <!-- 表格 -->
      <el-table
        size="small"
        :data="
          dataset.slice((currentPage - 1) * PageSize, currentPage * PageSize)
        "
        border
        stripe
        style="margin-bottom: 20px"
      >
        <el-table-column type="index" :index="1" label="序号"></el-table-column>
        <el-table-column
          label="名称"
          prop="name"
          align="center"
        ></el-table-column>
        <el-table-column
          label="简介"
          prop="introduce"
          align="center"
        ></el-table-column>
        <el-table-column
          label="封面"
          align="center"
        >
        <template slot-scope="scope">
            <div style="width:60px;border-radius:5px;height:50px;over-flow:hidden;margin:0 auto" >
                <img :src="scope.row.photo" alt=""  style="width:100%;height:60px">
            </div>
        </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
           
            <el-button size="mini" type="text" @click="toEnterHandler(scope.row)"
              >进入</el-button
            >
            <el-button size="mini" type="text" @click="editHandler(scope.row)"
              >修改</el-button
            >
             <el-button
              size="mini"
              slot="reference"
              @click="deleteHandler(scope.row)"
              type="text"
              style="color:red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格 -->
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="PageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { del, get, post } from "@/utils/request";
export default {
  // 页面数据
  data() {
    return {
      //参数，用于保存检索的参数
      param: {},
      // 默认显示第几页
      currentPage: 1,
      // 个数选择器（可修改）
      pageSizes: [2, 4, 6, 8],
      // 默认每页显示的条数（可修改）
      PageSize: 4,
      //定义的总数
      total: 0,
      //初始化总数据条目
      total: 0,
      //查询到的数据
      dataset: [],
    };
  },
  // 页面渲染的时候会调用的函数
  created() {
    this.loadDataset();
  },

  // 所有的方法
  methods: {
        toEnterHandler(){
      this.$router.push({
        path:"/file/datasetFile",     // 页面跳转的 - 与后端路由无关
        // query:{datasetId:row.id}
      })
    },

    //跳转
    toAddHandler() {
      this.$router.push("/file/datasetCreate");
    },
    //删除方法
    deleteHandler(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "http://39.106.31.187:8888/dataset/deleteById";
        del(url, { id: row.id }).then((response) => {
          // 1. 提示
          this.$message({ type: "success", message: response.message });
          // 2. 刷新
          this.loadDataset();
        });
      });
    },
    //修改方法
    editHandler(row) {
      this.$router.push({
        path: "/file/datasetCreate",
        query: row,
      });
    },
    //查询
    loadDataset() {
      let url = "http://39.106.31.187:8888/dataset/query";
      get(url, this.param).then((resp) => {
        this.dataset = resp.data;
        //总条数
        this.total = resp.data.length;
        console.log(this.total);
        console.log(resp);
      });
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
    },
  },
};
</script>
<style scoped>
</style>